package com.zyb.patient.view;


import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zyb.patient.R;

/**
 * 带进度条的WebView
 * 
 * 
 */
@SuppressWarnings("deprecation")
public class AboutProgressWebView extends WebView {

	private ProgressBar progressbar;
	private View mErrorView;
	private TextView mTtitleView;
	private String dTitle;

	public AboutProgressWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		progressbar = new ProgressBar(context, null,
				android.R.attr.progressBarStyleHorizontal);
		progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				5, 0, 0));
		addView(progressbar);
		// setWebViewClient(new WebViewClient(){});
		setWebChromeClient(new WebChromeClient());
		setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
				Log.e("shouldOverrideUrlLoading:", "shouldOverrideUrlLoading");
				hideErrorPage();
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				view.stopLoading();
				view.clearView();
				Log.e("onReceivedError:", "onReceivedError");
				showErrorPage();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				String title = view.getTitle();
				mTtitleView.setText(title);
				if (TextUtils.isEmpty(title) || "找不到网页".equals(title)) {
				    if(dTitle!=null){
				        mTtitleView.setText(dTitle); 
				    }else{
				        mTtitleView.setText(R.string.app_name);
				    }
				}
			}
		});

	}

	public void setErrorView(View errorView) {
		this.mErrorView = errorView;
	}

	public void setTitleView(TextView titleView,String dTitle) {
		this.mTtitleView = titleView;
		this.dTitle = dTitle;
	}

	public class WebChromeClient extends android.webkit.WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress == 100) {
				progressbar.setVisibility(GONE);
			} else {
				if (progressbar.getVisibility() == GONE)
					progressbar.setVisibility(VISIBLE);
				progressbar.setProgress(newProgress);
			}
			super.onProgressChanged(view, newProgress);
		}

	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
		lp.x = l;
		lp.y = t;
		progressbar.setLayoutParams(lp);
		super.onScrollChanged(l, t, oldl, oldt);
	}

	/**
	 * 显示自定义错误提示页面，用一个View覆盖在WebView
	 */
	protected void showErrorPage() {
		if (mErrorView != null) {
			Button button = (Button) mErrorView.findViewById(R.id.retry_btn);
			button.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					reload();
				}
			});
			mErrorView.setOnClickListener(null);
			mErrorView.setVisibility(View.VISIBLE);
			setVisibility(View.GONE);
		}
	}

	/**
	 * 隐藏错误界面
	 */
	protected void hideErrorPage() {
		mErrorView.setVisibility(View.GONE);
		setVisibility(View.VISIBLE);
	}
}
